package com.ruoyi.hospital.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.ruoyi.hospital.domain.HosWard;
import com.ruoyi.hospital.service.IHosWardService;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.HosBuildMapper;
import com.ruoyi.hospital.domain.HosBuild;
import com.ruoyi.hospital.service.IHosBuildService;

/**
 * 楼宇信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@Service
public class HosBuildServiceImpl implements IHosBuildService 
{
    @Autowired
    private HosBuildMapper hosBuildMapper;

    @Autowired
    private IHosWardService hosWardService;

    /**
     * 查询楼宇信息
     * 
     * @param buildId 楼宇信息主键
     * @return 楼宇信息
     */
    @Override
    public HosBuild selectHosBuildByBuildId(Long buildId)
    {
        return hosBuildMapper.selectHosBuildByBuildId(buildId);
    }

    /**
     * 查询楼宇信息列表
     * 
     * @param hosBuild 楼宇信息
     * @return 楼宇信息
     */
    @Override
    public List<HosBuild> selectHosBuildList(HosBuild hosBuild)
    {
        List<HosBuild> buildList = hosBuildMapper.selectHosBuildList(hosBuild);
        if (buildList.isEmpty()) return buildList;
        for (HosBuild build : buildList) {
            // 联查出楼宇拥有的病房数
            Long buildId = build.getBuildId();
            HosWard hosWard = new HosWard();
            hosWard.setBuildId(buildId);
            List<HosWard> wardList = hosWardService.selectHosWardList(hosWard);
            if (wardList.isEmpty()) build.setWardSum(0);
            else build.setWardSum(wardList.size());
        }
        return buildList;
    }

    /**
     * 新增楼宇信息
     * 
     * @param hosBuild 楼宇信息
     * @return 结果
     */
    @Override
    public int insertHosBuild(HosBuild hosBuild)
    {
        hosBuild.setBuildId(System.currentTimeMillis());
        return hosBuildMapper.insertHosBuild(hosBuild);
    }

    /**
     * 修改楼宇信息
     * 
     * @param hosBuild 楼宇信息
     * @return 结果
     */
    @Override
    public int updateHosBuild(HosBuild hosBuild)
    {
        return hosBuildMapper.updateHosBuild(hosBuild);
    }

    /**
     * 批量删除楼宇信息
     * 
     * @param buildIds 需要删除的楼宇信息主键
     * @return 结果
     */
    @Override
    public int deleteHosBuildByBuildIds(Long[] buildIds)
    {
        return hosBuildMapper.deleteHosBuildByBuildIds(buildIds);
    }

    /**
     * 删除楼宇信息信息
     * 
     * @param buildId 楼宇信息主键
     * @return 结果
     */
    @Override
    public int deleteHosBuildByBuildId(Long buildId)
    {
        return hosBuildMapper.deleteHosBuildByBuildId(buildId);
    }
}
